/** \file    bfstream.h
  * \brief   File-streams with binary interfaces.
  * \date    2003
  * \author  Fedor Chelnokov
  * \version $Header: /home/cscom1r4/cvsroot/cs/include/xtd/bfstream.h,v 1.1 2004/11/30 12:45:32 cscom1r4 Exp $
  */

#ifndef _bfstream_h_
#define _bfstream_h_

#include <fstream>
#include "bistream.h"
#include "bostream.h"

namespace xtd {

template <class E, class Tr = char_traits<E> >
class basic_bifstream : public basic_bistream<E, Tr>
{
public:
	basic_bifstream() : basic_bistream<E, Tr>(&_buf) {}
	explicit basic_bifstream(const char *filename, 
		std::ios_base::openmode mode = std::ios_base::in | std::ios::binary) : basic_bistream<E, Tr>(&_buf)
		{ open(filename, mode); }
	void open(const char *filename, 
		std::ios_base::openmode mode = std::ios_base::in | std::ios::binary) { 
		if (_buf.open(filename, mode | std::ios_base::in | std::ios::binary) == 0)
			setstate(failbit); 
	}
	void close() {
		if (_buf.close() == 0)
			setstate(failbit); 
	}
	bool is_open()
		{ return _buf.is_open(); }
	std::basic_filebuf<E, Tr> * rdbuf() const
		{ return const_cast<std::basic_filebuf<E, Tr>*>(&_buf); }

private:
	std::basic_filebuf<E, Tr> _buf;
};

typedef basic_bifstream<char, std::char_traits<char> > bifstream;

template <class E, class Tr = char_traits<E> >
class basic_bofstream : public basic_bostream<E, Tr>
{
public:
	basic_bofstream() : basic_bostream<E, Tr>(&_buf) {}
	explicit basic_bofstream(const char *filename, 
		std::ios_base::openmode mode = std::ios_base::out | std::ios::binary) : basic_bostream<E, Tr>(&_buf)
		{ open(filename, mode); }
	void open(const char *filename, 
		std::ios_base::openmode mode = std::ios_base::out | std::ios::binary) { 
		if (_buf.open(filename, mode | std::ios_base::out | std::ios::binary) == 0)
			setstate(failbit); 
	}
	void close() {
		if (_buf.close() == 0)
			setstate(failbit); 
	}
	bool is_open()
		{ return _buf.is_open(); }
	std::basic_filebuf<E, Tr> * rdbuf() const
		{ return const_cast<std::basic_filebuf<E, Tr>*>(&_buf); }

private:
	std::basic_filebuf<E, Tr> _buf;
};

typedef basic_bofstream<char, std::char_traits<char> > bofstream;

} //namespace xtd

#endif //_bfstream_h_
